home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 April / EnigmA AMIGA RUN 06 (1996)(G.R. Edizioni)(IT)[!][issue 1996-04][Skylink CD V].iso / internet / others / npns.lha / NPNS / sendnews.c < prev    next >
C/C++ Source or Header  |  1995-09-30  |  8KB  |  316 lines

  1.  
  2. /*
  3.  *    Program     sendnews
  4.  *    Programmer    N.d'Alterio
  5.  *    Date        06/08/95
  6.  *
  7.  *  Synopsis:    This program sends news which has been spooled by
  8.  *        postnews using NNTPpost from INETUtils. If not 
  9.  *              online then the program will exit and do nothing.
  10.  *              'Onlineness' is checked by trying to open the
  11.  *              TCP daytime port.
  12.  *
  13.  *              This program was adapted from the postnewsspool
  14.  *              package by James Burton which although useful
  15.  *        was bugged. It could often hang, it gave little
  16.  *              info as what it was doing, it left locks on the
  17.  *              spool dir and was over complicated.
  18.  * 
  19.  *              It is specifically compiled for my INET set up
  20.  *              i.e. spool dir AmiTCP:Usr/Spool/News
  21.  *                   NNTPPost  AmiTCP:bin/NNTPPost
  22.  *             failed arc AmiTCP:Usr/Spool/sendnews.failed
  23.  *
  24.  *        These may be changed using the ENV variables
  25.  *              PNS_SPOOLDIR and PNS_SEQFILE
  26.  *
  27.  *        If NNTPpost fails the article is appended to
  28.  *              the file AmiTCP:Usr/Spool/sendnews.failed or
  29.  *              the ENV variable PNS_FAILED_ARC. Each file has
  30.  *        a header with the date the file failed.
  31.  *
  32.  *  Command Line Arguments: None
  33.  *
  34.  *  Inputs:    Needs enviroment variables NNTPSERVER
  35.  *                                         PNS_SPOOLDIR   (opt)
  36.  *                                         PNS_NNTPPOST   (opt)
  37.  *                                         PNS_FAILED_ARC (opt)
  38.  *              
  39.  *
  40.  *  Outputs:    None
  41.  *
  42.  *  Variables:    BPTR    lock         -    lock on spool dir
  43.  *        BPTR    old_lock    -    lock on old current dir
  44.  *        char    *Buffer        -    string to build command in
  45.  *        char    *spooldir    -    spool dir name
  46.  *        char    *nntppost    -    nntppost prog name
  47.  *        char     *failed_arc    -    failed archive filename 
  48.  *        int    count        -    number of articles sent
  49.  *        int    exit_code    -    prog return code
  50.  *        FILE    *fptr        -    file pointer to TCP:daytime port
  51.  *            *fib        -    file info block pointer    
  52.  *
  53.  *  Functions:    save_failed        -    saves failed messages to failed archive
  54.  *
  55.  * $VER: sendnews.c 2.1 (07.08.95) $        
  56.  * $Log: sendnews.c $
  57.  * Revision 3.1  1995/09/09  20:44:51  daltern
  58.  * REWRITE: Now uses ADOS  functions
  59.  * does not need seqfile anymore
  60.  * will send anything that is the spool dir whatever the name
  61.  * failed arc now separates files with a date string containing
  62.  * the date the file failed
  63.  * New env variable user can now specify nntpposting prog
  64.  *
  65.  * Revision 2.3  1995/09/09  20:41:19  daltern
  66.  * now opens dos library version >37
  67.  *
  68.  * Revision 2.2  1995/08/27  21:32:56  daltern
  69.  * fixed a couple of typos
  70.  *
  71.  * Revision 2.1  1995/08/27  21:29:18  daltern
  72.  * Added some ENV var checking for paths and files
  73.  * Fixed a bug that left a file open
  74.  *
  75.  * Revision 1.3  1995/08/07  13:07:23  daltern
  76.  * added saving of failed messages
  77.  *
  78.  * Revision 1.2  1995/08/07  12:48:19  daltern
  79.  * Added version string
  80.  *
  81.  * Revision 1.1  1995/08/07  12:45:05  daltern
  82.  * Initial revision
  83.  *
  84.  *
  85.  */
  86.  
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89. #include <string.h>
  90. #include <dos/dos.h>
  91. #include <clib/dos_protos.h>
  92. #include <clib/exec_protos.h>
  93. #include <exec/types.h>
  94.  
  95. #define SPOOLDIR    "AmiTCP:Usr/Spool/News"
  96. #define FAILED_ARC  "AmiTCP:Usr/Spool/sendnews.failed"
  97. #define NNTPSERVER  NNTPServer
  98. #define NNTPPOSTER  "AmiTCP:bin/NNTPPost"
  99.  
  100. void save_failed( char *, char * );
  101.  
  102. extern struct Library *DOSBase;
  103. extern char *version = "$VER: sendnews 3.3 (30.09.95) Nick d'Alterio";
  104.  
  105. int main( void )
  106.  
  107. {
  108.  
  109.   int count;
  110.   int exit_code = 0;
  111.  
  112.   char *spooldir;
  113.   char *failed_arc;
  114.   char *nntppost;
  115.  
  116.   char Buffer[128];
  117.  
  118.   FILE *fptr;
  119.  
  120.   BPTR lock;
  121.   BPTR old_lock;
  122.  
  123.   struct FileInfoBlock *fib;
  124.  
  125. /*
  126.  *   Check if online.
  127.  */
  128.  
  129.   if ( ( fptr = fopen( "TCP:localhost/daytime", "r" ) )  == NULL ) {
  130.     fprintf( stderr, " You cant send news if you are not online !\n" );
  131.     exit(exit_code);
  132.   }   /* end if */
  133.   fclose( fptr );
  134.  
  135. /*
  136.  *   Read env variable to get NNTP server.
  137.  */
  138.         
  139.   if ( getenv("NNTPSERVER") == NULL ) {
  140.     fprintf( stderr, " You must specify a NNTP news server\n" );
  141.     exit(10);
  142.   }   /* end if */
  143.  
  144. /*
  145.  *   Read name of NNTP posting program 
  146.  */
  147.  
  148.   if ( ( nntppost = getenv( "PNS_NNTPPOST" ) ) == NULL ) {
  149.     nntppost = NNTPPOSTER;
  150.   }   /* end if */
  151.  
  152. /*
  153.  *   Read spool dir env variable.
  154.  */
  155.  
  156.   if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {
  157.     spooldir = SPOOLDIR;
  158.   }   /* end if */
  159.  
  160. /*
  161.  *    Read failed archive name from env variable.
  162.  */
  163.  
  164.   if ( ( failed_arc = getenv( "PNS_FAILED_ARC" ) ) == NULL ) {
  165.     failed_arc = FAILED_ARC;
  166.   }   /* end if */
  167.  
  168. /*
  169.  *  Open DOS library.
  170.  */
  171.  
  172.   if ( DOSBase = OpenLibrary( "dos.library", 37 ) ) {
  173.  
  174. /*
  175.  *   Lock the spool directory whilst posting.
  176.  */
  177.  
  178.     if ( lock = Lock( spooldir, ACCESS_READ ) ) {
  179.  
  180. /*
  181.  *   Change current dir to spool dir and alloc fib
  182.  */
  183.  
  184.           old_lock = CurrentDir( lock );
  185.       
  186.         if ( fib = AllocDosObject( DOS_FIB, NULL ) ) {
  187.  
  188. /*
  189.  *   Examine all the contents of spool dir and try to post
  190.  *   every message.
  191.  */
  192.    
  193.               count = 0;
  194.               Examine( lock, fib );
  195.               while( ExNext( lock, fib ) ) {
  196.  
  197.                 count++;
  198.                 sprintf( Buffer, "%s >NIL: %s", nntppost, fib->fib_FileName );
  199.     
  200.                 if ( !( system( Buffer ) ) ) {
  201.                     fprintf( stderr, " Posted article %d\n", count ); 
  202.                 } else {
  203.                     fprintf( stderr, " Failed to post article %d - saving \n", count );
  204.                     save_failed( fib->fib_FileName, failed_arc );
  205.                 }   /* end if */
  206.  
  207.                 if ( !( DeleteFile( fib->fib_FileName ) ) )
  208.                     PrintFault( IoErr(), fib->fib_FileName );
  209.                       
  210.               }   /* end while */
  211.  
  212.               FreeDosObject( DOS_FIB, fib );
  213.  
  214.         } else {
  215.  
  216.             exit_code = 20;
  217.     
  218.         }   /* end if alloc dos obj */
  219.  
  220.           CurrentDir( old_lock );
  221.         UnLock( lock );
  222.  
  223.     } else {
  224.  
  225.         PrintFault( IoErr(), NULL );
  226.         exit_code = 10;    
  227.  
  228.     }   /* end if lock */
  229.  
  230.     CloseLibrary( DOSBase );
  231.  
  232.   } else {
  233.  
  234.     fprintf( stderr, " Could not open dos.library > v37\n" );
  235.     exit_code = 20;
  236.  
  237.   }   /* end if library */
  238.                 
  239.   exit(exit_code);
  240.  
  241. }   /* end program sendnews */
  242.  
  243. /*========================================================================*
  244.                                   END
  245.  *========================================================================*/
  246.  
  247. /*========================================================================*
  248.                           BEGIN FUNCTION save_failed
  249.  *========================================================================*/
  250.  
  251. void save_failed( char *failed_file, char *arc_file )
  252.  
  253. {
  254.  
  255.   FILE *arc_fp;
  256.   FILE *msg_fp;
  257.  
  258.   struct DateStamp ds;
  259.   struct DateTime *dt;
  260.  
  261.   int ch;
  262.  
  263.   if ( !( arc_fp = fopen( arc_file, "a" ) ) ) {
  264.     fprintf( stderr, " Could not open failed archive\n" );
  265.   }   /* end if */
  266.  
  267.   if ( !( msg_fp = fopen( failed_file, "r" ) ) ) {
  268.     fprintf( stderr, " Could not open message file to backup\n" );
  269.     fclose( arc_fp );
  270.     return;
  271.   }   /* end if */
  272.  
  273. /*
  274.  *   Print a header for failed file containing date.
  275.  */
  276.  
  277.   dt = ( struct DateTime *)malloc( sizeof( struct DateTime ) );
  278.  
  279.   dt->dat_StrDay  = (char *)malloc( 20 * sizeof( char ) );
  280.   dt->dat_StrTime = (char *)malloc( 20 * sizeof( char ) );
  281.   dt->dat_StrDate = (char *)malloc( 20 * sizeof( char ) );
  282.   
  283.   dt->dat_Format =  FORMAT_DOS;
  284.   dt->dat_Flags  =  NULL;
  285.  
  286.   DateStamp( &ds );
  287.   dt->dat_Stamp = ds;
  288.   DateToStr( dt );
  289.  
  290.   fprintf( arc_fp, "\n\n ********************* %s %s %s ******************* \n\n",
  291.                                 dt->dat_StrTime, 
  292.                                 dt->dat_StrDay, 
  293.                                 dt->dat_StrDate );
  294.  
  295.   free( dt->dat_StrTime );
  296.   free( dt->dat_StrDay );
  297.   free( dt->dat_StrDate );
  298.   free( dt );
  299.  
  300. /*
  301.  *   Now copy msg to archive
  302.  */
  303.  
  304.   while ( ( ch = fgetc( msg_fp ) ) != EOF ) fputc( ch, arc_fp );
  305.  
  306.  
  307.   fclose( arc_fp );
  308.   fclose( msg_fp );
  309.   return;
  310.  
  311. }   /* end function save_failed */
  312.  
  313. /*========================================================================*
  314.                                  END save_failed
  315.  *========================================================================*/
  316.